package com.cakeclient.clientsoap;


import com.cakeclient.clientsoap.Exception.SoapClientException;
import com.cakeclient.clientsoap.Exception.SoapComException;

import java.util.HashMap;
import java.util.Map;

public abstract class Method<T> {
    private SoapClient soapClient;
    private String methodName;
    private Map<String, Object> parameters = new HashMap<String, Object>();

    public Method(SoapClient soapClient, String methodName, Map<String, Object> parameters) {
        this.soapClient = soapClient;
        this.methodName = methodName;
        if (parameters != null) {
            this.parameters = new HashMap<String, Object>(parameters);
        }

    }

    @Override
    public String toString() {
        return new String(soapClient.getNameSpace() + methodName);
    }

    public void addParameters(Map<String, Object> parameters) {
        parameters.putAll(parameters);
    }

    public void setParameters(Map<String, Object> parameters) {
        if (parameters != null)
            this.parameters = parameters;
    }

    public void setParameter(String key, Object value) {
        this.parameters.clear();
        this.parameters.put(key, value);
    }


    public abstract T getResult(Class<T> type) throws SoapComException, SoapClientException;


    public abstract T getResult() throws SoapComException, SoapClientException;


    protected Object executeCall() throws SoapComException, SoapClientException {
        Object resp = null;
        Call call = makeCall();
        call.addParameter(parameters);
        resp = call.executeCallResponse();
        return resp;
    }

    private Call makeCall() {
        return new Call(soapClient.getNameSpace() + methodName, methodName, soapClient.getNameSpace(), soapClient.getURI(), soapClient.isDotNet());
    }
}


